<?php
/**
 * Insert loose functions that are needed by the several objects here
 *
 *
 *
 *
 * @author Ralph de Boom <php5-sidnepp@mijndomein.nl>
 * @author Peter Keizer <php5-sidnepp@mijndomein.nl>
 * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License v3
 * @copyright http://www.mijndomein.nl/
 * @version 0.5
 * @link http://php5-sidnepp.svn.sourceforge.net/viewvc/php5-sidnepp/trunk/dev/docs/index.html
 * @see http://php5-sidnepp.svn.sourceforge.net/viewvc/php5-sidnepp/trunk/dev/docs/php5-sidnepp/SidnTools.html
 * @package php5-sidnepp
 */
class SidnTools 
{
	/**
	 * This will parse and fill in a string with [!VAR[key]] tags in it
	 *
	 * @param string $string text to parse
	 * @param array $vars array with variables to place into text
	 * @param boolean $clean clean the text of unresolved variables?
	 * @return string
	 */
	public static function parseVars($string, $vars, $clean = false)
	{
		if (is_array($vars))
		{
			foreach ($vars as $key => $val)
			{
				$string = preg_replace('/(\[\!VAR\['.$key.'\]\])/ms', $val, $string);
			}
			if ($clean)
			{
				$string = preg_replace('/(\[\!VAR\[.*?\]\])/ms', '', $string);
			}
		}
		return $string;
	}

	/**
	 * This wil get the parsed xml from the xml directory
	 *
	 * @param string $name filename of the xml
	 * @param array $vars array with variables for the xml. Where array key represents the variable name
	 * @return string/boolean your xml contents
	 */
	public static function getXml($name, $vars)
	{		
		if ($xml = file_get_contents(SidnEppConfig::RUN_PATH.SidnEppConfig::XML_PATH.'/'.$name))
		{
			$xml = self::parseVars($xml, $vars, true);
			return $xml;
		}
		else
		{
			return false;
		}
	}
}
